package kk.yiyi.safedesign.sms;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

/**
 * 监听sim状态改变的广播，返回sim卡的状态， 有效或者无效。
 * 双卡中只要有一张卡的状态有效即返回状态为有效，两张卡都无效则返回无效。
 */
public class SimStateReceiver extends BroadcastReceiver {
    public final static String ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED";
    private OnSimChangeListener onSimSwitchListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
            int state = tm.getSimState();
            if (onSimSwitchListener != null) {
                onSimSwitchListener.simValid(state == TelephonyManager.SIM_STATE_READY);
            }
        }
    }

    public void setOnSimSwitchListener(OnSimChangeListener onSimSwitchListener) {
        this.onSimSwitchListener = onSimSwitchListener;
    }

    public interface OnSimChangeListener {
        void simValid(boolean valid);
    }
}
